// 编码映射样例
// const encodingMap:any = {
//     "'": "%27-27%",
//     "\"": "%22-22%",
//     ",": "%2C-2C%",
//     ";": "%3B-3B%"
// };
import map from "./charEncoding.json";
const encodingMap: any = map.encoding;

// 编码函数
function encodeString(input: string): string {
  return Object.keys(encodingMap).reduce((encodedStr, char) => {
    const replacement = encodingMap[char];
    return encodedStr.replace(new RegExp(`\\${char}`, "g"), replacement);
  }, input);
}

// 解码函数
function decodeString(input: string): string {
  const reverseEncodingMap: Record<string, string> = Object.fromEntries(
    Object.entries(encodingMap).map(([key, value]) => [value, key])
  );

  return Object.keys(reverseEncodingMap).reduce((decodedStr, encodedChar) => {
    const originalChar = reverseEncodingMap[encodedChar];
    return decodedStr.replace(new RegExp(encodedChar, "g"), originalChar);
  }, input);
}

export { encodeString, decodeString };
